<?php

class msutaz{
	
	public function sutazeorganizatora(){
		$id = $_SESSION['user']->id;
		conndb::getInst()->query("SELECT DISTINCT a.* FROM sutaz a 
							INNER JOIN sutazne_kolo b on (a.id=b.id_sutaz)  
							WHERE b.id_sutazna_kategoria NOT IN (SELECT c.id_sutazna_kategoria FROM sutazne_kolo c WHERE c.meno IS NOT NULL ) 
							AND a.idOrganizator='".$id."' AND b.aktivne_kolo IS NULL ");
		$sutaze = conndb::getInst()->getRowsAsArrayOfObjects();	
		conndb::getInst()->query("SELECT  a.* FROM sutaz a WHERE a.idOrganizator=\"".$id."\" AND a.id NOT IN (SELECT c.id_sutaz FROM sutazne_kolo c) ");
		$bezkola = conndb::getInst()->getRowsAsArrayOfObjects();
		if (!empty($bezkola)){
			foreach($bezkola as $bk){
				array_push($sutaze,$bk);	
			}
		}
		return $sutaze;
		
	}
	
	public function odstran($id){
		foreach($id as $jedno){
			conndb::getInst()->query("DELETE FROM kategoria_hodnotenia a WHERE a.id_sutazne_kolo IN (SELECT b.id FROM sutazne_kolo b WHERE a.id_sutaz=".$jedno.")");
			conndb::getInst()->query("DELETE FROM sutazne_kolo WHERE id_sutaz=".$jedno."");
			conndb::getInst()->query("DELETE FROM sutaz WHERE id=".$jedno."");
		}
		
	}
	
	public function aktsutaze(){
		$id = $_SESSION['user']->id;
		conndb::getInst()->query("SELECT DISTINCT a.* FROM sutaz a INNER JOIN sutazne_kolo b on (a.id=b.id_sutaz) WHERE a.idOrganizator=\"".$id."\" AND b.aktivne_kolo IS NOT NULL ");
		$kat = conndb::getInst()->getRowsAsArrayOfObjects();
		return $kat;
	}
	
	public function kategoriesutaze($idsutaz){
		conndb::getInst()->query("SELECT DISTINCT d.id, d.meno, d.popis FROM sutazne_kolo e INNER JOIN sutazna_kategoria d on (e.id_sutazna_kategoria=d.id) WHERE e.id_sutaz=\"".$idsutaz."\"  ");
		$kat = conndb::getInst()->getRowsAsArrayOfObjects();
		return $kat;
	}
	
	public function kolasutaze(){
		conndb::getInst()->query("SELECT e.meno,d.Meno,e.popis  FROM sutazne_kolo e INNER JOIN sutazna_kategoria d on (e.id_sutazna_kategoria=d.id) WHERE e.id_sutaz=\"".$_SESSION['idupravsu']."\" AND e.meno!='' ORDER BY d.Meno ");
		$kola = conndb::getInst()->getRowsAsArrayOfObjects();
		return $kola;
	}
	
	public function hodnotiacekategorie(){
		conndb::getInst()->query("SELECT DISTINCT k.id, k.Meno, k.Popis, p.Meno AS kat FROM sutazne_kolo e INNER JOIN kategoria_hodnotenia d on (e.id=d.id_sutazne_kolo) 
		INNER JOIN typ_kategoria_hodnotenia k on (k.id=d.id_kategoria_hodnotenia) INNER JOIN sutazna_kategoria p on (p.id=e.id_sutazna_kategoria) WHERE e.id_sutaz=\"".$_SESSION['idupravsu']."\" ORDER BY e.id ");
		$kola = conndb::getInst()->getRowsAsArrayOfObjects();
		return $kola;
	}
	
	public function scitacisystem(){
		conndb::getInst()->query("SELECT b.*, c.Meno AS kat, a.meno FROM sutazne_kolo a 
								INNER JOIN scitaci_system b on (a.id_scitaci_system=b.id) 
								INNER JOIN sutazna_kategoria c on (c.id=a.id_sutazna_kategoria )
								WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" ");
		
		return conndb::getInst()->getRowsAsArrayOfObjects();
	}
	
	public function aktivuj(){
		conndb::getInst()->query("UPDATE sutazne_kolo a SET a.aktivne_kolo='1'   WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" AND a.id_scitaci_system IS NOT NULL AND a.id_sutazna_kategoria IS NOT NULL ");
	}
	
	public function aktivtest(){
		conndb::getInst()->query("SELECT a.id_sutazna_kategoria FROM sutazne_kolo a WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" AND a.meno IS NULL AND a.id_scitaci_system IS NULL ");
		$kola =conndb::getInst()->getRowsAsArrayOfObjects();
		$test =true;
		foreach($kola as $kol){
			conndb::getInst()->query("SELECT a.id_sutazna_kategoria FROM sutazne_kolo a WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" AND a.meno IS NOT NULL AND a.id_sutazna_kategoria='".$kol->id_sutazna_kategoria."'");
			$pom = conndb::getInst()->getRowAsObject();
			if (empty($pom)){$test=false;}
		}
		conndb::getInst()->query("SELECT a.id_sutazna_kategoria FROM sutazne_kolo a WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" AND a.meno IS NOT NULL AND a.id_scitaci_system IS NULL ");	
		$pom = conndb::getInst()->getRowAsObject();
		if (empty($pom)){}else{$test=false;}
		return $test;
		
		
	}
	
}
?>